package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.domain.inOutStock.PickerTaskOrder;
import com.canaan.business.domain.vo.PickerTaskOrderVo;
import com.canaan.business.domain.vo.YardageGoodsTaskOrderConfirmVo;
import com.canaan.business.service.inOutStock.IPickerTaskOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.config.RuoYiConfig;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.file.FileUploadUtils;
import com.canaan.common.utils.file.MimeTypeUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 拣货任务单Controller
 *
 * @author ruoyi
 * @date 2023-02-02
 */
@Api(tags = "拣货任务单")
@RestController
@RequestMapping("/inOutStock/pickerTaskOrder")
public class PickerTaskOrderController extends BaseController {
    @Resource
    private IPickerTaskOrderService pickerTaskOrderService;

    /**
     * 查询拣货任务单列表
     */
    @ApiOperation(value = "查询拣货任务单列表")
    @GetMapping("/list")
    public TableDataInfo list(PickerTaskOrder pickerTaskOrder) {
        startPage();
        List<PickerTaskOrder> list = pickerTaskOrderService.selectPickerTaskOrderList(pickerTaskOrder);
        return getDataTable(list);
    }
    @ApiOperation(value = "根据出库单明细查询拣货单")
    @GetMapping("/listByOutOrderId/{outOrderId}")
    public AjaxResult listByOutOrderId(@PathVariable("outOrderId") Long outOrderId){
        List<PickerTaskOrder> list = pickerTaskOrderService.selectPickerTaskOrderByOutOrderId(outOrderId);
        return success(list);
    }

    /**
     * 导出拣货任务单列表
     */
    @ApiOperation(value = "导出拣货任务单列表")
    @Log(title = "拣货任务单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PickerTaskOrder pickerTaskOrder) {
        List<PickerTaskOrder> list = pickerTaskOrderService.selectPickerTaskOrderList(pickerTaskOrder);
        ExcelUtil<PickerTaskOrder> util = new ExcelUtil<PickerTaskOrder>(PickerTaskOrder.class);
        util.exportExcel(response, list, "拣货任务单数据");
    }

    /**
     * 获取拣货任务单详细信息
     */
    @ApiOperation(value = "获取拣货任务单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pickerTaskOrderService.selectPickerTaskOrderById(id));
    }



    /**
     * 新增拣货任务单
     */
    @ApiOperation(value = "新增拣货任务单")
    @Log(title = "拣货任务单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PickerTaskOrder pickerTaskOrder) {
        return toAjax(pickerTaskOrderService.insertPickerTaskOrder(pickerTaskOrder));
    }

    /**
     * 修改拣货任务单
     */
    @ApiOperation(value = "修改拣货任务单")
    @Log(title = "拣货任务单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PickerTaskOrder pickerTaskOrder) {
        return toAjax(pickerTaskOrderService.updatePickerTaskOrder(pickerTaskOrder));
    }

    /**
     * 删除拣货任务单
     */
    @ApiOperation(value = "删除拣货任务单")
    @Log(title = "拣货任务单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pickerTaskOrderService.deletePickerTaskOrderByIds(ids));
    }


    /**
     * 确认拣货
     */
    @ApiOperation(value = "确认拣货")
    @Log(title = "确认拣货", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody PickerTaskOrderVo params){
        return toAjax(pickerTaskOrderService.confirmOrComplete(params,false));
    }



    /**
     * 库区图片上传
     */
    @Log(title = "上传签名图片", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadSign")
    public AjaxResult uploadSign(@RequestParam("signfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String url = FileUploadUtils.upload(RuoYiConfig.getStoreHouseAreaPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("signUrl", url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    @ApiOperation(value = "查询出库单是否全部完成")
    @GetMapping("/confirmSign/{no}")
    public AjaxResult confirmSign(@PathVariable("no") String no){
        return success(pickerTaskOrderService.confirmSign(no));
    }
    /**
     * 完成拣货
     */
    @ApiOperation(value = "完成拣货")
    @Log(title = "完成拣货", businessType = BusinessType.UPDATE)
    @PostMapping("/complete")
    public AjaxResult complete(@RequestBody PickerTaskOrderVo params){
        return toAjax(pickerTaskOrderService.confirmOrComplete(params,true));
    }
    /**
     * 确认码货
     */
    @ApiOperation(value = "确认码货")
    @Log(title = "确认码货", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmYard")
    public AjaxResult confirmYard(@RequestBody YardageGoodsTaskOrderConfirmVo params){
        return toAjax(pickerTaskOrderService.confirmOrCompleteYard(params,false));
    }

    /**
     * 完成码货
     */
    @ApiOperation(value = "完成码货")
    @Log(title = "完成码货", businessType = BusinessType.UPDATE)
    @PostMapping("/completeYard")
    public AjaxResult completeYard(@RequestBody YardageGoodsTaskOrderConfirmVo params){
        return toAjax(pickerTaskOrderService.confirmOrCompleteYard(params,true));
    }


}
